package projet.camping.domaine;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import projet.camping.domaine.Habitation.TypeHabitation;

@Entity @Table(name="JOUEUR",schema="CAMPING")
public class Joueur implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8541286382736108801L;


	@Id @GeneratedValue
	private int id;
	
	
	@ManyToOne
	@JoinColumn(nullable=false)
	private User user;
	
	
	@OneToMany(cascade = CascadeType.ALL)
	@JoinColumn(nullable=false,name="JOUEUR_ID")
	@MapKey(name="type")
	private Map<TypeHabitation,Habitation> mesHabitations = new HashMap<TypeHabitation,Habitation>();

	
	@OneToMany(cascade = CascadeType.ALL)
	@JoinColumn(nullable=false,name="JOUEUR_ID")
	private List<Tir> mesTirs = new ArrayList<Tir>();
	
	@Column(nullable=false)
	private boolean pret = false;
	
	public boolean isPret() {
		return pret;
	}

	public void setPret() {
		this.pret = true ;
	}


	public Joueur(){
	}
	
	public Joueur( User user) {
		this.user = user;
		this.initialiseHabitation();
	}
	private void initialiseHabitation() {
		this.mesHabitations.put(Habitation.TypeHabitation.Mobile_Home, new Habitation(Habitation.TypeHabitation.Mobile_Home));
		this.mesHabitations.put(Habitation.TypeHabitation.Bungalow, new Habitation(Habitation.TypeHabitation.Bungalow));
		this.mesHabitations.put(Habitation.TypeHabitation.CampingCar, new Habitation(Habitation.TypeHabitation.CampingCar));
		this.mesHabitations.put(Habitation.TypeHabitation.TenteLuxe, new Habitation(Habitation.TypeHabitation.TenteLuxe));
		this.mesHabitations.put(Habitation.TypeHabitation.TenteIgloo, new Habitation(Habitation.TypeHabitation.TenteIgloo));
	}

	public int getId() {
		return id;
	}

	public User getUser() {
		return user;
	}



	public Map<TypeHabitation,Habitation> getMesHabitations() {
		return mesHabitations;
	}


	public List<Tir> getMesTirs() {
		return mesTirs;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Joueur other = (Joueur) obj;
		if (id != other.id)
			return false;
		if(!this.getUser().getPseudo().equals(other.getUser().getPseudo()))
			return false;
		return true;
	}


	public Habitation attaquer(int x,int y,Joueur aJoueur){
		mesTirs.add(new Tir(x,y));
		return aJoueur.seFaitAttaquer(x, y);
		
		
	}
	
public Habitation seFaitAttaquer(int x,int y){
		
		for(TypeHabitation th: mesHabitations.keySet()){
			for(int i=0;i<mesHabitations.get(th).getTaille();){
				Habitation h = mesHabitations.get(th);
				if(h.isHorizontal()){
					if(y!=h.getY())
						break;
					if(x >= h.getX() && x <= h.getX()+h.getTaille()-1)
						h.incrCptTouche();
						return h;
				}else{			
					if(x!=h.getX())
						break;
					if(y >= h.getY() && y <= h.getY() + h.getTaille()-1)
						h.incrCptTouche();
						return h;
						}
			}		
		}
		return null;
	}
	


	public boolean isOutOfPastis(){
		for(TypeHabitation th:this.getMesHabitations().keySet()){
			if(!this.getMesHabitations().get(th).estDetruit())
				return false;
		}
		return true;
	}

	

}
